import axios from 'axios'
import store from '@/store' // 导入vuex
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API// 请求的基地址ip
})

// 请求拦截器
service.interceptors.request.use(function(config) {
  const token = store.state.user.token
  // console.log(token)
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
},
error => {
  // console.log(error)
  return Promise.reject(error)
}
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    if (!response.data.success) { return Promise.reject(new Error(response.data.massage)) }
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
